Compound Patterns
A lot of patterns work together very well to form compound patterns. Shown is a UML class diagram that implements various design patterns to form a compound pattern. Here are some notes describing how and why this design was arrived at.
- Polymorphism was introduced by the use of the
Quackable
interface. This makes a number of design patterns available to us. - The decorator pattern is used to allow us to track the number of times the method quack() is called. Using the
decorator pattern means we don't have to modify
Duck
andDuckCall
QuackDecorator
implementsQuackable
so it can be referenced as aQuackable
typeQuackDecorator
has aQuackable
instance variable so it can be passed aQuackable
typeQuackDecorator
is the first object in the chain, so it'squack()
method is called first. This allows it to update a counter variable, then call thequack()
of itsQuackable
instance variable
- To use the
Goose
class, the adapter pattern is utilised so that we can convert the call toGoose
'shonk()
method toQuackables
quack()
method. Note thatGooseAdapter
implementsQuackable
so we can refer to all our objects as the sameQuackable
type. - A factory pattern method is used to create our objects, so that we separate object creation from object use.